package com.anuo.app.modules.study.studyjobschedular;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.anuo.app.common.utils.ExceptionUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 定义一个执行任务的job , 并且出现异常会解除和 trigger的绑定, 这样后续这个job都不会执行了
 * 异常的处理见: http://www.quartz-scheduler.org/documentation/quartz-2.x/examples/Example6.html
 */
public class HelloJob implements Job {
    private static final Logger log = LoggerFactory.getLogger(HelloJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {

            log.error("MyJob  is start ..................");
            log.error("Hello quzrtz  " +
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));
            log.error("MyJob  is end .....................");

        } catch (Exception e){
            JobExecutionException jobExecutionException = new JobExecutionException(e);
            // Quartz will automatically unschedule
            // all triggers associated with this job
            // so that it does not run again
            jobExecutionException.setUnscheduleAllTriggers(true);
            //获取异常信息
            String msg=jobExecutionException.getMessage();
            String msgs = ExceptionUtil.getStackTrace(jobExecutionException);
            throw jobExecutionException;

        }

    }


}
